package com.corechina.apex_osswords.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.domain.dao.CoreEquipmentCheckResultMapper;
import com.corechina.apex_osswords.domain.dao.CorePollingEquipmentMapper;
import com.corechina.apex_osswords.domain.entity.CorePollingEquipment;
import com.corechina.apex_osswords.domain.vo.CorePollingEquipmentVo;
import com.corechina.apex_osswords.service.ICorePollingEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 巡检设备表 服务实现类
 * </p>
 *
 * @author cc
 * @since 2023-03-09
 */
@Service
public class CorePollingEquipmentServiceImpl extends ServiceImpl<CorePollingEquipmentMapper, CorePollingEquipment> implements ICorePollingEquipmentService {

    @Autowired
    private CorePollingEquipmentMapper corePollingEquipmentMapper;

    @Autowired
    private CoreEquipmentCheckResultMapper coreEquipmentCheckResultMapper;


    @Override
    public Response getList(Integer planId, Integer equipmentId, Integer status, Integer type, Integer pageIndex, Integer pageSize) {
        IPage<CorePollingEquipmentVo> pollingEquipmentVoIPage = corePollingEquipmentMapper.getList(new Page(pageIndex, pageSize), planId, equipmentId, status, type);
        return Response.ok(pollingEquipmentVoIPage.getRecords(), pollingEquipmentVoIPage.getTotal());
    }

    @Override
    public Response getOne(Integer id) {
        CorePollingEquipmentVo corePollingEquipmentVo = corePollingEquipmentMapper.getOne(id);
        if (corePollingEquipmentVo != null) {
            corePollingEquipmentVo.setCoreEquipmentCheckResultList(coreEquipmentCheckResultMapper.getListByEqId(corePollingEquipmentVo.getId()));
        }
        return Response.ok(corePollingEquipmentVo);
    }

    @Override
    public Response getPollingEquipmentBySerialNumber(String serialNumber, Integer planId) {
        Integer id = corePollingEquipmentMapper.getId(planId, serialNumber);
        if (id == null) {
            return Response.error("未查找到巡检计划");
        }
        return this.getOne(id);
    }
}
